
var app = getApp()
Page({
  data: {
    bookInfoId: "",
    coverPic: "",
    name: "",
    maxScore: "",
    author: "",
    isbn: "",
    publisher: "",
    price: ""
  },


  comeBack () {
    wx.navigateBack()
  },



  //数据绑定
  bindname(e) {
    this.setData({
      name: e.detail.value
    })
  },

  bindisbn(e) {

    this.setData({
      isbn: e.detail.value
    })
  },

  bindprice(e) {
    this.setData({
      price: e.detail.value
    })
    
  },

  bindpublisher(e) {
    this.setData({
      publisher: e.detail.value
    })
    
  },



  getChangesave() {



    if (this.data.name == "") {
      Taro.showToast({
        title: "书名不能为空",
        icon: "none"
      })

      return
    }


    if (this.data.isbn == "") {
      Taro.showToast({
        title: "isbn不能为空",
        icon: "none"
      })

      return
    }

    if (this.data.price == "") {
      Taro.showToast({
        title: "价格不能为空",
        icon: "none"
      })

      return
    }

    if (this.data.publisher == "") {
      Taro.showToast({
        title: "出版社不能为空",
        icon: "none"
      })

      return
    }

    var params = {
      bookInfoId: this.data.bookInfoId,
      name: this.data.name,
      author: this.data.author,
      maxScore: this.data.maxScore,
      isbn: this.data.isbn,
      price: this.data.price,
      publisher: this.data.publisher,
      coverPic: this.data.coverPic
    }

    this.adMinapi(params)

  },


  adMinapi(params) {
    wx.showLoading({
      title: "保存中",
      icon: "none"
    })
    wx.request({
      url: "https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/InsertAdminBookInfo/auth",
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.hideLoading()
          wx.showToast({
            title: "保存成功",
            icon: "none",
            duration: 2000
          })

          setTimeout(function () {
            wx.navigateBack()
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      }
    })
  },



  getDetail(bookInfoId) {

    var _this = this
    var params = {
      bookInfoId: bookInfoId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/findAdminBookInfoById/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.setData({
            coverPic: res.data.data.coverPic,
            name: res.data.data.name,
            maxScore: res.data.data.maxScore,
            author: res.data.data.author,
            isbn: res.data.data.isbn,
            publisher: res.data.data.publisher,
            price: res.data.data.price
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      }
    })
  },

  changeBtn () {
    var _this = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0]
        _this.upLoadurl(res.tempFilePaths[0])
      }
    })
  },


  upLoadurl (url) {
    var that = this
    // var list = this.data.coverPic
    app.uploadFile('img', url, 'oneHour(v3.0)/otherImg/', function (result) {
      console.log(result)
      that.setData({
        coverPic: result
      })
    })
  },
  onLoad (options) {
    console.log(options.bookInfoId)
    this.setData({ 
      bookInfoId: options.bookInfoId
    })

    this.getDetail(options.bookInfoId)
  }
})